package com.moore.Servlet;

import com.alibaba.fastjson2.JSON;
import com.moore.model.User;
import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;
import com.moore.net.HttpServlet;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class UserServlet extends HttpServlet {
    private static final Map<String,Object> map = new HashMap<>();

    @Override
    public byte[] doGet(HttpRequest request, HttpResponse response) {
        String nickName = request.getParams().get("nickname");
        if (nickName.equals(User.getName())){
            return JSON.toJSONBytes(map);
        }
        return null;
    }

    public static void getValue(Object object) {
        try {
            Class<?> clazz = object.getClass();
            Field[] fields =clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object value = field.get(object);
                map.put(name, value);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
